package zhu.areas.hatchnav.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import zhu.areas.dic.data.ArticleTopic;
import zhu.areas.dic.data.ArticleType;
import zhu.areas.hatchnav.data.Article;
import zhu.areas.hatchnav.service.IArticlePubService;
import zhu.areas.hatchnav.service.IArticleRankService;
import zhu.areas.user.service.IUserService;
import zhu.common.service.IDicService;

import com.opensymphony.xwork2.ActionSupport;

/*
 * 发布文章
 */
public class PubArticleAction extends ActionSupport {
	private IArticlePubService articlePubService;
	private IArticleRankService articleRankService;
	private IDicService dicService;

	private Integer articleID;
	private String articleTitle;
	private String articleContent;
	private int publisherID;// 发布人ID
	private String publisherName;// 发布人姓名
	private Date pubTime;
	private int visitNum = 0;// 初始文章的访问量

	private String attempt = "";// 发布文章提示信息

	private List<Article> articleTopList;// top文章列表
	private Integer topCount = 5;

	private List<ArticleType> articleTypeList;
	private Integer artTypeID;// 文章类型：政策解读、职业规划、热点评论、中心新闻、通知公告
	private ArticleType articleType;

	private List<ArticleTopic> articleTopicList;
	private Integer artTopicID;
	private ArticleTopic articleTopic;

	// 跳转到文章发布页面
	public String toPubArticle() {
		// 查询排名top5的文章列表
		this.articleTopList = this.articleRankService.findTopArticlesByVisitNum(topCount);
		this.articleTopicList = this.dicService.getAllValueAndId("ArticleTopic");
		this.articleTypeList = this.dicService.getAllValueAndId("ArticleType");
		return SUCCESS;
	}

	// 点击提交按钮，发布文章
	public String pubArticle() {

		System.out.println("excute pubArticle");
		// 查询排名top5的文章列表
		this.articleTopList = this.articleRankService.findTopArticlesByVisitNum(topCount);

		Article article = new Article();
		convertStringToArticle(article);

		if (this.articlePubService.addArticle(article)) {
			attempt = "发布文章成功";
			articleID = article.getArticleID();
			System.out.println("发布文章成功，文章ID：" + article.getArticleID());
			return SUCCESS;
		} else {
			attempt = "发布文章失败";
			System.out.println("发布文章失败。。。");
			return ERROR;
		}
	}

	// 将从页面获取地数据封装成Article对象
	private void convertStringToArticle(Article article) {

		System.out.println("excute convertStringToArticle");

		System.out.println("文章标题：" + this.getArticleTitle());
		System.out.println("文章内容：" + this.getArticleContent());
		article.setArticleTitle(this.getArticleTitle());
		article.setArticleContent(this.getArticleContent());

		// 从session中获取用户的ID和姓名
		publisherID = (Integer) ServletActionContext.getRequest().getSession()
				.getAttribute("userID");
		publisherName = ServletActionContext.getRequest().getSession()
				.getAttribute("userName").toString();
		System.out.println("发布人ID：" + publisherID);
		System.out.println("发布人姓名：" + publisherName);
		article.setPublisher(publisherID);
		article.setPublisherName(publisherName);

		System.out.println("文章类型：" + artTypeID);
		article.setArtType(artTypeID);
		System.out.println("文章主题：" + artTopicID);
		article.setArticleTopic((ArticleTopic) this.dicService.getValueById(
				"ArticleTopic", "artTopicID", artTopicID));

		pubTime = new Date();
		article.setPubTime(pubTime);
		article.setVisitNum(visitNum);
	}

	public IArticlePubService getArticlePubService() {
		return articlePubService;
	}

	public void setArticlePubService(IArticlePubService articlePubService) {
		this.articlePubService = articlePubService;
	}

	public IArticleRankService getArticleRankService() {
		return articleRankService;
	}

	public void setArticleRankService(IArticleRankService articleRankService) {
		this.articleRankService = articleRankService;
	}

	public IDicService getDicService() {
		return dicService;
	}

	public void setDicService(IDicService dicService) {
		this.dicService = dicService;
	}

	public Integer getArticleID() {
		return articleID;
	}

	public void setArticleID(Integer articleID) {
		this.articleID = articleID;
	}

	public String getArticleTitle() {
		return articleTitle;
	}

	public void setArticleTitle(String articleTitle) {
		this.articleTitle = articleTitle;
	}

	public String getArticleContent() {
		return articleContent;
	}

	public void setArticleContent(String articleContent) {
		this.articleContent = articleContent;
	}

	public int getPublisherID() {
		return publisherID;
	}

	public void setPublisherID(int publisherID) {
		this.publisherID = publisherID;
	}

	public String getPublisherName() {
		return publisherName;
	}

	public void setPublisherName(String publisherName) {
		this.publisherName = publisherName;
	}

	public Date getPubTime() {
		return pubTime;
	}

	public void setPubTime(Date pubTime) {
		this.pubTime = pubTime;
	}

	public int getVisitNum() {
		return visitNum;
	}

	public void setVisitNum(int visitNum) {
		this.visitNum = visitNum;
	}

	public List<ArticleType> getArticleTypeList() {
		return articleTypeList;
	}

	public void setArticleTypeList(List<ArticleType> articleTypeList) {
		this.articleTypeList = articleTypeList;
	}

	public String getAttempt() {
		return attempt;
	}

	public void setAttempt(String attempt) {
		this.attempt = attempt;
	}

	public List<Article> getArticleTopList() {
		return articleTopList;
	}

	public void setArticleTopList(List<Article> articleTopList) {
		this.articleTopList = articleTopList;
	}

	public Integer getTopCount() {
		return topCount;
	}

	public void setTopCount(Integer topCount) {
		this.topCount = topCount;
	}

	public List<ArticleTopic> getArticleTopicList() {
		return articleTopicList;
	}

	public void setArticleTopicList(List<ArticleTopic> articleTopicList) {
		this.articleTopicList = articleTopicList;
	}

	public ArticleTopic getArticleTopic() {
		return articleTopic;
	}

	public void setArticleTopic(ArticleTopic articleTopic) {
		this.articleTopic = articleTopic;
	}

	public Integer getArtTypeID() {
		return artTypeID;
	}

	public void setArtTypeID(Integer artTypeID) {
		this.artTypeID = artTypeID;
	}

	public ArticleType getArticleType() {
		return articleType;
	}

	public void setArticleType(ArticleType articleType) {
		this.articleType = articleType;
	}

	public Integer getArtTopicID() {
		return artTopicID;
	}

	public void setArtTopicID(Integer artTopicID) {
		this.artTopicID = artTopicID;
	}

}
